package com.lanou.fooddemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;

import com.lanou.fooddemo.R;
import com.lanou.fooddemo.adapter.SameCityListAdapter;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.dao.FoodDatabaseTool;

import java.util.ArrayList;
import java.util.List;

public class BasketActivity extends Activity {

    private ListView basketListView;
    private List<DataBean> dataBeans;
    private SameCityListAdapter adapter;
    private Handler handler;
    private FoodDatabaseTool databaseTool;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basket);

        initView();
        getDataOfDatabase();
    }
    private void initView() {
        basketListView = (ListView) findViewById(R.id.basketListView);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                adapter = new SameCityListAdapter(BasketActivity.this, dataBeans);
                basketListView.setAdapter(adapter);
                databaseTool = new FoodDatabaseTool(BasketActivity.this);
                return true;
            }
        });
        dataBeans = new ArrayList<DataBean>();
        adapter = new SameCityListAdapter(BasketActivity.this, dataBeans);
        basketListView.setAdapter(adapter);
        databaseTool = new FoodDatabaseTool(BasketActivity.this);
    }
    private void getDataOfDatabase() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                dataBeans = databaseTool.selectAllBasket();
                if (null != dataBeans) {
                    Log.i("liuyann", "dataBeans size " + dataBeans.size());
                }
                Message msg = handler.obtainMessage();
                handler.sendMessage(msg);
            }
        });
        thread.start();
    }
}
